package com.gd.iot.energy.service.superadmin.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.superadmin.LogPOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.superadmin.AdminPO;
import com.gd.iot.energy.po.superadmin.LogPO;
import com.gd.iot.energy.po.superadmin.LogPOWithBLOBs;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.superadmin.dto.LogVOWithBLOBs;
import com.gd.iot.energy.pojo.superadmin.vo.LogPageVO;
import com.gd.iot.energy.service.superadmin.LogService;
import com.gd.iot.energy.util.CustomBeanUtils;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 日志管理
 */
@Service
@Slf4j
public class LogServiceImpl extends ServiceImpl<LogPOMapper, LogPOWithBLOBs> implements LogService {

    /**
     * 表头配置文件
     */
    private static final String LOG_LIST_COLUMN_FILE_NAME = "superadmin/log_list.column";

    @Resource
    private LogPOMapper logPOMapper;

    @Override
    @Async
    public void saveLog(LogPOWithBLOBs logPOWithBLOBs) {
        try {
            save(logPOWithBLOBs);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            log.error(JSONUtil.toJsonStr(logPOWithBLOBs));
        }
    }

    @Override
    public PageVO<LogPageVO> getPageLogVOs(String operator, String operation, Integer pageSize, Integer pageNum, String orderColumn, String orderType) {
        IPage<LogPOWithBLOBs> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LogPOWithBLOBs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(operator)) {
            queryWrapper.like("operator", operator.trim());
        }
        if (StrUtil.isNotBlank(operation)) {
            queryWrapper.like("operation", operation.trim());
        }

        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        IPage<LogPOWithBLOBs> logPOPage = logPOMapper.selectPage(page, queryWrapper);
        PageVO<LogPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<LogPageVO> roleVOList = convertToAdminPageVOList(logPOPage.getRecords());
        pageVO.setData(roleVOList);
        pageVO.setTotal(logPOPage.getTotal());
        pageVO.setPages(logPOPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(LOG_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * po转vo
     *
     * @param records 记录
     * @return
     */
    private List<LogPageVO> convertToAdminPageVOList(List<LogPOWithBLOBs> records) {
        List<LogPageVO> res = new ArrayList<>(records.size());

        LogPageVO logPageVO;
        long i = 1;
        for (LogPOWithBLOBs record : records) {
            logPageVO = new LogPageVO();
            logPageVO.setId(record.getId());
            logPageVO.setSerialNum(i);
            logPageVO.setOperation(record.getOperation());
            logPageVO.setOperator(record.getOperator());
            logPageVO.setIp(record.getIp());
            logPageVO.setContent(record.getContent());
            logPageVO.setCreateDate(DateUtil.formatDate(record.getCreateDate()));

            res.add(logPageVO);
            i++;
        }
        return res;
    }

    @Override
    public LogVOWithBLOBs getLog(Integer id) {
        LogPOWithBLOBs logPOWithBLOBs = logPOMapper.selectById(id);
        if (logPOWithBLOBs == null || SystemConsts.BooleanEnum.TRUE.value.equals(logPOWithBLOBs.getIsDel())) {
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }
        LogVOWithBLOBs logVOWithBLOBs = new LogVOWithBLOBs();
        CustomBeanUtils.convert(logPOWithBLOBs, logVOWithBLOBs);
        return logVOWithBLOBs;
    }

    @Override
    public boolean delete(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //逻辑删除
        QueryWrapper<LogPOWithBLOBs> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        LogPOWithBLOBs logPO = new LogPOWithBLOBs();
        logPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = logPOMapper.update(logPO,queryWrapper);

        return num > 0;
    }

    @Override
    public boolean clearAll() {
        //逻辑删除
        QueryWrapper<LogPOWithBLOBs> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("is_del", SystemConsts.BooleanEnum.FALSE.value);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        LogPOWithBLOBs logPO = new LogPOWithBLOBs();
        logPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        logPOMapper.update(logPO,queryWrapper);
        return true;
    }
}
